let getHttpLocal = (obj) => {
	obj.HTTPLOCAT = process.env.APP_ORIGIN;
};
let bindToGlobal = (obj, key) => {
	getHttpLocal(obj);
	if (typeof window[key] === 'undefined') {
		window[key] = {};
	}
	for (let i in obj) {
		window[key][i] = obj[i]
	}
}
bindToGlobal({HTTPLOCAT: ''}, '_const');

export function parseTime(time, cFormat) {
	if (arguments.length === 0) {
		return null;
	}
	if (time === undefined || time === null)
		return '';

	const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
	let date;
	if (typeof time == 'object') {
		date = time;
	} else {
		if (('' + time).length === 10) time = parseInt(time) * 1000;

		date = new Date(time);
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	};
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key];
		if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1];
		if (result.length > 0 && value < 10) {
			value = '0' + value;
		}
		return value || 0;
	});
	return time_str;
}

export function objectMerge(target, source) {
	if (typeof target !== 'object') {
		target = {};
	}
	if (Array.isArray(source)) {
		return source.slice();
	}
	for (const property in source) {
		if (source.hasOwnProperty(property)) {
			const sourceProperty = source[property];
			if (typeof sourceProperty === 'object') {
				target[property] = objectMerge(target[property], sourceProperty);
				continue;
			}
			target[property] = sourceProperty;
		}
	}
	return target;
}

export function isLeftIE9() {

	var b_version = navigator.appVersion;
	var version = b_version.split(";");
	//navigator.appVersion 在火狐浏览器 没有版本号
	if (version.length > 1) {
		var trim_Version = version[1].replace(/[ ]/g, "");
		if (trim_Version == "MSIE6.0") {
			return true;
		} else if (trim_Version == "MSIE7.0") {
			return true;
		} else if (trim_Version == "MSIE8.0") {
			return true;
		}
		return false;

	} else {
		return false;
	}
}

export function getParamsUrl() {
	let hashDeatail = location.hash.split("?"),
		hashName = hashDeatail[0].split("#")[1],//路由地址
		params = hashDeatail[1] ? hashDeatail[1].split("&") : [],//参数内容
		query = {};
	for (var i = 0; i < params.length; i++) {
		var item = params[i].split("=");
		query[item[0]] = item[1]
	}
	return {
		path: hashName,
		query: query
	}
}

export default _const;

